package com.wchat.controller;

import com.wchat.dataobject.ProductCategory;
import com.wchat.dataobject.ProductInfo;
import com.wchat.enums.ProductStatusEnum;
import com.wchat.service.CategoryService;
import com.wchat.service.ProductService;
import com.wchat.utils.ResultVoUtils;
import com.wchat.vo.*;
import com.wchat.vo.ProductInfoVo;
import com.wchat.vo.ProductVo;
import com.wchat.vo.ResultVo;
import org.apache.log4j.spi.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yangtianrui on 17/8/10.
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(BuyerProductController.class);
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVo<ResultVo> list() {
        // 获取在线的商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        List<Integer> categoryList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        //  获取商品类目>
        List<ProductVo> productVoList = new ArrayList<>();
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryList);
        for(ProductCategory productCategory:productCategoryList)
        {
            ProductVo productVo = new ProductVo();
            productVo.setCategoryType(productCategory.getCategoryType());
            productVo.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVo> productInfoVoList = new ArrayList<>();
            for(ProductInfo productInfo:productInfoList)
            {
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType()))
                {
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo,productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }
            }
            productVo.setFoodsList(productInfoVoList);
            productVoList.add(productVo);
        }
        return ResultVoUtils.success(productVoList);
    }

        //  传统方法获取
////        for(ProductInfo productInfo :productInfoList)
////        {
////            categoryList.add(productInfo.getCategoryType());
////        }
//        //  新版本 lamada 表达式 获取商品分类的类目ID
//        categoryList = productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
//        List< ProductCategory> productCategoryList= categoryService.findByCategoryTypeIn(categoryList);
//        List<ProductVo> productVoList = new ArrayList<>();
//        // 遍历类目
//        for (ProductCategory catetory : productCategoryList) {
//            ProductVo productVo = new ProductVo();
//            // 设置商品的分类情况
//             productVo.setCategoryName(catetory.getCategoryName());
//             productVo.setCategoryType(catetory.getCategoryType());
//            List<ProductInfoVo> productInfoVoList = new ArrayList<>();
//            // 遍历类目下商品详情
//        for (ProductInfo productInfo : productInfoList) {
//            //  设置 商品详细情况
////            if(productInfo.getCategoryType().equals(catetory.getCategoryType()))
////            {
//                ProductInfoVo productInfoVo = new ProductInfoVo();
//                BeanUtils.copyProperties(productInfo,productInfoVo);
//                productInfoVoList.add(productInfoVo);
////            }
//        }
//            productVo.setFoodsList(productInfoVoList);
//            productVoList.add(productVo);
//    }
//        ResultVo resultVo = new ResultVo();
//        resultVo.setData(productVoList);
//        resultVo.setCode(ProductStatusEnum.ONLINE.getCode());//  返回给前端的状态码
//        resultVo.setMsg(ProductStatusEnum.ONLINE.getValue()); //  前端的结果 展示
//        return resultVo;
//
////        ResultVo productVo = new ResultVo();
////        ProductVo categoryVo = new ProductVo();
////        ProductInfoVo categoryListVo = new ProductInfoVo();
////        categoryVo.setCategoryName("热销榜");
////        categoryVo.setCategoryType(3);
////
////        categoryListVo.setProductId("123");
////        categoryListVo.setProductName("皮蛋廋肉粥");
////        categoryListVo.setProductPrice(new BigDecimal(2.3));
////        categoryListVo.setProductDescription("好吃的粥");
////        categoryListVo.setProductIcon("http://www.baidu.com");
////        List<ProductInfoVo> list = Arrays.asList(categoryListVo);
////        categoryVo.setFoodsList(list);
////        List<ProductVo> categoryVoList = Arrays.asList(categoryVo);
////        productVo.setCode(0);
////        productVo.setMsg("成功");
////        productVo.setData(categoryVoList);
////
////
////        return  productVo;

    //}

}
